package com.dc.contoller;
import com.alibaba.fastjson.JSON;
import com.dc.base.model.BaseModel;


import com.dc.service.NoticeService;
import com.dc.service.impl.NoticeServiceImpl;
import com.dc.service.impl.UserService;
import com.dc.service.impl.UserServiceImpl;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * Created by Acer on 2019/9/28.
 */

@WebServlet(name="NoticeServlet",urlPatterns={"/addNotice.do","/updateNotice.do","/deleteNotice.do","/selectNotice.do"})
public class NoticeContoller extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //做业务逻辑
        String url=req.getRequestURI();
        url=url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
        Method method =null;
        try{
            method = getClass().getDeclaredMethod(url,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,req,resp);
        }catch (Exception e){
            System.out.println("java反射失败");
            e.printStackTrace();
        }
    }
    public void addNotice(HttpServletRequest req, HttpServletResponse resp){
        try{
            //定义一个数组
            Object[]  params = new Object[3];
            req.setCharacterEncoding("utf-8");
            //获取用户传入的参数
            params[0]=req.getParameter("notice_title");
            params[1]=req.getParameter("notice_content");

            params[2]=new Date();



            NoticeService noticeService = new NoticeServiceImpl();
            BaseModel baseModel = new BaseModel();
            baseModel=noticeService.addNotice(params);
            String baseModelStr=JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();

        }catch (Exception e){
            System.out.println("新增用户失败!");
            e.printStackTrace();
        }
    }
    public void deleteNotice(HttpServletRequest req, HttpServletResponse resp){
        try{
            NoticeService noticeService = new NoticeServiceImpl();
            req.setCharacterEncoding("utf-8");
            int id=Integer.valueOf(req.getParameter("notice_id"));

            BaseModel baseModel =noticeService.deleteNotice(id);
            String baseModelStr=JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();
        }catch (Exception e){
            System.out.println("删除用户信息执行失败");
            e.printStackTrace();
        }





    }
    public void selectNotice(HttpServletRequest req, HttpServletResponse resp){
        NoticeService noticeService = new NoticeServiceImpl();
        BaseModel baseModel=new BaseModel();
        try {
            baseModel=noticeService.selectNotice(null);
            String baseModelStr= JSON.toJSONString(baseModel);
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("application/json;charset=utf-8");

            resp.getWriter().write(baseModelStr);
            resp.getWriter().flush();
            resp.getWriter().close();
        }catch (Exception e){
            System.out.println("查询所有用户信息接口出错");
            e.printStackTrace();
        }

    }

}

